package com.distribute.framework.core.web.helper;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.core.env.Environment;
import org.yaml.snakeyaml.Yaml;

public class ConfigHelper
{
	public static Properties m_props=new Properties();
	public static String  m_charset="GBK";
	public static Yaml m_yaml = new Yaml();
	public static Map m_root = new HashMap();
	public static Environment environment;
	
	static{
		try{
			if(ClassLoader.class.getResource("/application.properties")!=null)
			{
				m_charset=FileHelper.getCharset(ClassLoader.class.getResourceAsStream("/application.properties"));
				m_props.load(ClassLoader.class.getResourceAsStream("/application.properties"));	
			}
			else if(Thread.currentThread().getContextClassLoader().getResource("/application.properties")!=null)
			{
				m_charset=FileHelper.getCharset(Thread.currentThread().getContextClassLoader().getResourceAsStream("/application.properties"));
				m_props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/application.properties"));	
			}
			else if(ConfigHelper.class.getResource("/application.properties")!=null){
				m_charset=FileHelper.getCharset(ConfigHelper.class.getResourceAsStream("/application.properties"));
				m_props.load(ConfigHelper.class.getResourceAsStream("/application.properties"));
			}
		} catch (Exception ex){
			System.out.println("ERR - undefined application.properties");
		}
		try {
			if(ClassLoader.class.getResource("/application.yml")!=null)
			{
				InputStream as = ClassLoader.class.getResourceAsStream("/application.yml");
				if(as!=null){
					m_root = (Map) m_yaml.load(as);
				}
			}
			else if(Thread.currentThread().getContextClassLoader().getResource("/application.yml")!=null)
			{
				InputStream as = Thread.currentThread().getContextClassLoader().getResourceAsStream("/application.yml");
				if(as!=null){
					m_root = (Map) m_yaml.load(as);
				}	
			}
			else if(ConfigHelper.class.getResource("/application.yml")!=null){
				InputStream as = ConfigHelper.class.getResourceAsStream("/application.yml");
				if(as!=null){
					m_root = (Map) m_yaml.load(as);
				}
			}
        } catch (Exception e) {
        	e.printStackTrace();
        	System.out.println("ERR - undefined application.yml");
        }
	}
	
	public static void main(String...strings){	
		System.out.println(GetString("redis.session.index"));	
	}
	
	public static Boolean contains(String key){
		return GetString(key)!=null;
	}
	
	public static String GetString(String key)
	{
		if(environment==null && SpringBeanHelper.springContext!=null){
			environment = SpringBeanHelper.springContext.getEnvironment();
		}
		if(environment!=null){
			String value = environment.getProperty(key);
			if(value!=null){
				return value;
			}
		}
		String value=m_props.getProperty(key);	
		try{
			if(value!=null){
				value=new String(value.getBytes("ISO-8859-1"),m_charset);
			}else if(m_root!=null){
				String[] items = key.split("\\.");
				Map map = m_root;
				for(int i=0;i<items.length-1;i++){
					String item = items[i];
					if(map.get(item)!=null && map.get(item) instanceof Map){
						map = (Map) map.get(item);
					}else{
						map = null;
						break;
					}
				}
				if(map!=null){
					Object v = map.get(items[items.length-1]);
					if(v!=null){
						return v.toString();
					}
				}
			}
		}catch(Exception ex){
			System.out.println("获取配置文件属性["+key+"]时出错 - "+ ex.getMessage());
		}
		return value;
	}	
	
	public static Long GetLong(String key)
	{
		Long l=null;
		try{
			l=Long.parseLong(GetString(key));
		}catch(Exception ex){
			//ex.printStackTrace();
			System.out.println("获取配置文件属性["+key+"]时出错 - "+ ex.getMessage());
		}
		return l;
	}
	
	public static Integer GetInteger(String key)
	{
		Integer i=null;
		try{
			i=Integer.parseInt(GetString(key));
		}catch(Exception ex){
			//ex.printStackTrace();
			System.out.println("获取配置文件属性["+key+"]时出错 - "+ ex.getMessage());
		}
		return i;
	}
	
	public static Boolean GetBoolean(String key)
	{
		Boolean b=false;
		try{
			String s=GetString(key);			
			b=Boolean.valueOf(s)||s.equals("1");
		}catch(Exception ex){
			//ex.printStackTrace();
			System.out.println("获取配置文件属性["+key+"]时出错 - "+ ex.getMessage());
		}
		return b;
	}
	
	public static String config(String key){		
		return GetString(key);
	}

	public static Environment getEnvironment() {
		return environment;
	}

	public static void setEnvironment(Environment environment) {
		ConfigHelper.environment = environment;
	}
}